<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class GoodsCategory extends Model
{

    protected $table = 'goods_category';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U';
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 获取列表
     * @param array $where
     * @return mixed
     */
    public static function getList($where = [])
    {
        $db = self::where([]);
        if (isset($where['cate_name']) && !empty($where['cate_name'])) {
            $db->where('cate_name', trim($where['cate_name']));
        }
        if (isset($where['pid']) && $where['pid'] !== null) {
            $db->where('pid', (int)($where['pid']));
        }

        $result = $db->orderBy('id', 'asc')->get()->toArray();
        return $result;
    }

    /**
     * 获取详情
     * @param array $where
     * @return mixed
     */
    public static function getInfo($where = [])
    {
        $db = self::where([]);
        if (isset($where['id']) && $where['id'] !== null) {
            $db->where('id', (int)($where['id']));
        }

        $result = $db->orderBy('id', 'asc')->first();
        return $result;
    }

    /**
     * 添加分类
     * @param array $where
     * @return mixed
     */
    public static function add($info)
    {
        $time = time();
        $info['updated_at'] = $time;
        $info['created_at'] = $time;

        return self::insertGetId($info);
    }

    /**
     * 编辑分类
     * @param $id
     * @param $info
     * @return mixed
     */
    public static function edit($id, $info)
    {
        $info['updated_at'] = time();

        return self::where(['id' => $id])->update($info);
    }

    /**
     * 删除分类
     * @param $id
     * @return mixed
     */
    public static function del($id)
    {
        return self::where(['id' => $id])->delete();
    }
}